# -*- coding: utf-8 -*-
import scrapy
import json
import urllib


class Xq2Spider(scrapy.Spider):
    name = 'xq2'
    allowed_domains = ['xueqiu.com']
    start_urls = ['https://xueqiu.com/#/property']


    def start_requests(self):
        url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'
        cookie_ = '_ga=GA1.2.678144559.1555391372; device_id=2c34e96b00dad81bebc9df8ce0729835; aliyungf_tc=AQAAABs4Um8jPg8A6KFyqzEpAme4y8Fi; xq_a_token=363aa481eb7c8b5ec33a22dad82f9b50a811a76d; xq_a_token.sig=-IsDKkHnatxXFjssWaGxDZ4FLsg; xq_r_token=6982254134692e2b8e4ecda2e571b3a01d723e5f; xq_r_token.sig=yITO4PkYzpoSN9Y9BtE1h19RPDo; _gid=GA1.2.1416988890.1556712979; u=831556712979587; Hm_lvt_1db88642e346389874251b5a1eded6e3=1555397274,1555901898,1556712980,1556713012; _gat=1; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1556713178'
        cookie = {i.split('=')[0]: i.split('=')[1] for i in cookie_.split('; ')}
        yield scrapy.Request(
            url,
            callback=self.parse,
            cookies=cookie
        )


    def parse(self, response):
        json_d = json.loads(response.text)
        info_l = json_d['list']
        for one_info in  info_l:
            data = json.loads(one_info['data'])
            item = {}
            item['标题'] = data['title']
            item['描述'] = data['description']
            item['详情链接'] = 'https://xueqiu.com' + data['target']
            yield item
            print(item)

        next_max_id = json_d['next_max_id']
        params = {
            "since_id": "-1",
            "max_id": next_max_id,
            "count": "15",
            "category": "111",
        }
        api_ = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?'

        api_url = api_ + urllib.parse.urlencode(params)
        yield scrapy.Request(
            api_url,
            callback=self.parse
        )